using System.Collections.Generic;
using UnityEngine;

public class CardMO{
    static List<Card> _cardDataList = null;//所有卡牌数据

    private static void InitData()
    {
        if (_cardDataList != null) return;
        //读取装备数据
        _cardDataList = Utils.LoadByJson<List<Card>>(Application.dataPath, "cardList.json");
        if (_cardDataList == null) _cardDataList = new List<Card>();

        //重新创建单个大项属性，保存为json后转回来，就变成父类的对象
        for(int i = 0;i < _cardDataList.Count;i++){
            for(int j = 0;j < _cardDataList[i].skill.Count;j++){
                var propertyList = _cardDataList[i].skill[j].propertyList;
                var assembly = Utils.GetCurAssembly();
                _cardDataList[i].skill[j] = (SkillBase)assembly.CreateInstance(_cardDataList[i].skill[j].className); 
                _cardDataList[i].skill[j].InitData(propertyList);
            }
        }
    }
    public static List<Card> GetCardList(){
        InitData();
        return _cardDataList;
    }
    /// <summary>
    /// 保存卡牌列表
    /// </summary>
    public static void SaveCardDataList(){
        if(_cardDataList == null) return;
        Utils.SaveByJson<List<Card>>(Application.dataPath, "cardList.json",_cardDataList);
    }
    /// <summary>
    /// 保存卡牌
    /// </summary>
    /// <param name="cardData"></param>
    /// <param name="index"></param>
    /// <returns></returns>
    public static bool SaveCard(Card cardData,int index)
    {
        //新增卡牌的保存
        if(index == _cardDataList.Count) {
            _cardDataList.Add(cardData);
        }
        else{
            if (index < 0 || index >= _cardDataList.Count) return false;
            _cardDataList[index] = cardData;
        }
        SaveCardDataList();
        return true;
    }
    /// <summary>
    /// 删除卡牌
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public static bool DelCard(int index){
        if (index < 0 || index >= _cardDataList.Count) return false;
        LTLocalization.DelKey(_cardDataList[index].name);
        BiologyMO.RemoveAllPointCard(_cardDataList[index].id);
        _cardDataList.RemoveAt(index);
        //ResetCardId();
        SaveCardDataList();
        LTLocalization.SaveDataToCSV();
        return true;
    }
    /// <summary>
    /// 重置卡牌id
    /// </summary>
    public static void ResetCardId(){
        if(_cardDataList == null) return;
        for(int i = 0;i < _cardDataList.Count;i++){
            _cardDataList[i].id = "Card_"+(i + 1);
            ResetCardName(_cardDataList[i]);
        }
    }
    /// <summary>
    /// 重置卡牌名字键值
    /// </summary>
    /// <param name="card"></param>
    public static void ResetCardName(Card card)
    {
        var newName = card.id + "_name";
        if(newName != card.name)
        {
            var isContain = LTLocalization.IsContain(card.name);
            if (isContain)
            {
                LTLocalization.ReplaceKey(newName,card.name);
                LTLocalization.DelKey(card.name);
            }
            card.name = newName;
        }
    }
    public static int GetCardIndex(Card card){
        InitData();
        return _cardDataList.IndexOf(card);
    }
    /// <summary>
    /// 获取指定id的数据
    /// </summary>
    /// <param name="cardId"></param>
    /// <returns></returns>
    public static Card GetCardById(string cardId){
        InitData();
        foreach(var item in _cardDataList){
            if(item.id == cardId) return item;
        }
        return null;
    }
}
